import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: leetcode98
 * @author: JCM
 * @create: 2023-02-23 22:05
 * @description:
 **/

class TreeNode {
    TreeNode left;
    int val;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
    }
}
public class Main {
    public boolean isValidBST(TreeNode root) {
        // 创建一个 list 存放二叉树中的值
        List<Integer> list = new ArrayList<>();
        inorder(root,list);
        System.out.println(list.toString());
        int[] arr = new int[list.size()];
        int index = 0;
        // 遍历 list，并把数字存入数组中
        for(int n : list) {
            arr[index++] = n;
        }
        boolean ret = true;
        for(int i = 0; i < arr.length-1; i++) {
            // 不符合从小到大的规则，ret  置为 false
            if(arr[i+1] <= arr[i]) {
                ret = !ret;
                break;
            }
        }
        return ret;

    }
    // 中序遍历
    private void inorder(TreeNode root,List<Integer> list) {
        if(root == null) {
            return;
        }
        inorder(root.left,list);
        list.add(root.val);
        inorder(root.right,list);
    }
}
